const { Op } = require("sequelize");
const receptionBill = require("../database/model-reception");
const receptionBillDraft = require("../database/model-reception-draft");
const visitingService = require("./service-visiting");
const vehicleService = require("./service-vehicle");

const BillList = async (ctx) => {
  const { billNo, id } = ctx.request.query || {};
  let conditions = {};
  if (billNo) {
    conditions = {
      where: {
        billNo,
      },
    };
  } else if (id) {
    conditions = {
      where: {
        id,
      },
    };
  }

  const { count: draftCount, rows: drafts } = await receptionBillDraft.findAndCountAll(conditions);
  const { count: billCount, rows: bills } = await receptionBill.findAndCountAll(
    conditions
  );

  ctx.success({
    list: [
      ...drafts.map(({ id, status, billNo, content }) => {
        const { application, ...restDraft } = JSON.parse(content);

        return {
          ...application,
          ...restDraft,
          id,
          status,
          billNo,
        };
      }),
      ...bills,
    ],
    draftCount,
    billCount,
    total: draftCount + billCount,
    msg: "success",
  });
};
BillList.method = "get";
BillList.mark = "单据列表";
module.exports = BillList;
